//
//  LoginViewController.m
//  OrderAssistant
//
//  Created by chendong on 2020/8/24.
//  Copyright © 2020 chendong. All rights reserved.
//

#import "LoginViewController.h"
#import "HomeViewController.h"
#import "FCUUID.h"
#import "NetTools.h"
#import <SVProgressHUD.h>
#import "UserData.h"
#import <AFNetworking.h>
@interface LoginViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *zhTF;
@property(nonatomic,strong)UITextField *mmTF;
@property (nonatomic,strong) UIView *mbV;
@property (nonatomic,strong) UIView *srBgV;
@property (nonatomic,strong) UITextField *srBgVTF;
@property (nonatomic,strong) UIButton *jzBtn;
@property (nonatomic,copy) NSString *webURL;
@end

@implementation LoginViewController

-(UIView *)mbV{
    if (!_mbV) {
        _mbV = [[UIView alloc]initWithFrame:self.view.bounds];
        _mbV.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [self.view addSubview:_mbV];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mbVAction:)];
        [_mbV addGestureRecognizer:tap];
    }
    return _mbV;
}
-(void)mbVAction:(UITapGestureRecognizer *)tap{
    [self.view endEditing:YES];
//    self.mbV.hidden = YES;
//    self.srBgV.hidden = YES;
}
-(UIView *)srBgV{
    if (!_srBgV) {
        _srBgV = [[UIView alloc]initWithFrame:CGRectMake(50, 0, SCREENWIDTH-100, 270)];
        _srBgV.center = CGPointMake(SCREENWIDTH/2, SCREENHEIGHT/2);
        _srBgV.backgroundColor = [UIColor whiteColor];
        _srBgV.layer.cornerRadius = 5;
        _srBgV.clipsToBounds = YES;
        [self.view addSubview:_srBgV];
        UILabel *titleL = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, _srBgV.frame.size.width, 50)];
        titleL.text = @"请输入IP";
        titleL.textAlignment = NSTextAlignmentCenter;
        titleL.numberOfLines = 0;
        [_srBgV addSubview:titleL];
        UILabel *slL = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(titleL.frame), 0, 50)];
        slL.text = @"IP:";
        [_srBgV addSubview:slL];
        UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(slL.frame), CGRectGetMaxY(titleL.frame), _srBgV.frame.size.width-10-CGRectGetMaxX(slL.frame), 50)];
        NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"ipaddress"];
        tf.text = str.length==0?@"":str;//42.233.35.83:8089
//        tf.keyboardType = UIKeyboardTypeDecimalPad;
        tf.layer.borderWidth = 1;
        tf.layer.borderColor = [UIColor lightGrayColor].CGColor;
        tf.layer.cornerRadius = 5;
        tf.clipsToBounds = YES;
        tf.placeholder = @"请输入数量";
        UIView *lV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 50)];
        tf.leftView = lV;
        tf.leftViewMode = UITextFieldViewModeAlways;
        [_srBgV addSubview:tf];
        self.srBgVTF = tf;
        UILabel *egL = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(slL.frame), CGRectGetMaxY(tf.frame), tf.frame.size.width, 70)];
        egL.text = @"说明：IP地址无值时，工作模式为外网；IP地址有值时，工作模式为连接该IP的内网，内网IP如192.168.0.21";
        egL.font = [UIFont systemFontOfSize:14];
        egL.textColor = RGBCOLOR(150, 150, 150);
        egL.numberOfLines = 0;
        [_srBgV addSubview:egL];
        UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancleBtn.frame = CGRectMake(10, CGRectGetMaxY(egL.frame)+10, _srBgV.frame.size.width/2-20, 40);
        cancleBtn.layer.cornerRadius = 5;
        cancleBtn.clipsToBounds = YES;
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cancleBtn.backgroundColor = RGBCOLOR(108, 185, 251);
        [_srBgV addSubview:cancleBtn];
        [cancleBtn addTarget:self action:@selector(cancleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *quedingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        quedingBtn.frame = CGRectMake(CGRectGetMaxX(cancleBtn.frame)+20, CGRectGetMaxY(egL.frame)+10, _srBgV.frame.size.width/2-20, 40);
        quedingBtn.layer.cornerRadius = 5;
        quedingBtn.clipsToBounds = YES;
        [quedingBtn setTitle:@"确定" forState:UIControlStateNormal];
        [quedingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        quedingBtn.backgroundColor = RGBCOLOR(108, 185, 251);
        [_srBgV addSubview:quedingBtn];
        [quedingBtn addTarget:self action:@selector(quedingBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _srBgV;
}
-(void)cancleBtnAction:(UIButton *)btn{
    self.mbV.hidden = YES;
    self.srBgV.hidden = YES;
    [self.view endEditing:YES];
}
-(void)quedingBtnAction:(UIButton *)btn{
    self.mbV.hidden = YES;
    self.srBgV.hidden = YES;
    [self.view endEditing:YES];
}
-(void)longAAction:(UILongPressGestureRecognizer *)lo{
    self.mbV.hidden = NO;
    self.srBgV.hidden = NO;
    [self.srBgVTF becomeFirstResponder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENWIDTH/414.0*332)];
    imgV.userInteractionEnabled = YES;
    imgV.image = [UIImage imageNamed:@"首页_01.png"];
    [self.view addSubview:imgV];
    UILongPressGestureRecognizer *longA = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAAction:)];
    [imgV addGestureRecognizer:longA];
    
    [self.view addSubview:[self setupTFWithPlh:@"请输入您的账号" frame:CGRectMake(50, CGRectGetMaxY(imgV.frame)+10, SCREENWIDTH-100, 30)]];//账号
    [self.view addSubview:[self setupTFWithPlh:@"请输入您的密码" frame:CGRectMake(50, CGRectGetMaxY(imgV.frame)+90, SCREENWIDTH-100, 30)]];//密码
    
    UIView *jzzhBgV = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.mmTF.frame)-100, CGRectGetMaxY(self.mmTF.frame)+10, 100, 30)];
    [self.view addSubview:jzzhBgV];
    UIButton *jzzhBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    jzzhBtn.frame = CGRectMake(0, 5, 20, 20);
    [jzzhBtn setBackgroundImage:[UIImage imageNamed:@"首页_10.png"] forState:UIControlStateNormal];
    [jzzhBtn setBackgroundImage:[UIImage imageNamed:@"hahah.png"] forState:UIControlStateSelected];
    [jzzhBgV addSubview:jzzhBtn];
    self.jzBtn = jzzhBtn;
    [jzzhBtn addTarget:self action:@selector(jzzhBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *jzzhL = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(jzzhBtn.frame), 0, jzzhBgV.frame.size.width-CGRectGetMaxX(jzzhBtn.frame), 30)];
    jzzhL.text = @"记住密码";
    [jzzhBgV addSubview:jzzhL];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(self.mmTF.frame.origin.x, CGRectGetMaxY(jzzhBgV.frame)+10, self.mmTF.frame.size.width, 50);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.layer.cornerRadius = 5;
    loginBtn.clipsToBounds = YES;
    loginBtn.backgroundColor = RGBCOLOR(108, 185, 252);
    [self.view addSubview:loginBtn];
    [loginBtn addTarget:self action:@selector(loginBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *ldrjL = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREENHEIGHT-60, SCREENWIDTH, 50)];
    ldrjL.textColor = RGBCOLOR(100, 100, 100);
    ldrjL.text = @"乐点软件\nwww.zhenyea.com";
    ldrjL.textAlignment = NSTextAlignmentCenter;
    ldrjL.font = [UIFont systemFontOfSize:10];
    ldrjL.numberOfLines = 0;
    [self.view addSubview:ldrjL];
    
    UILabel *bbhL = [[UILabel alloc]initWithFrame:CGRectMake(20, SCREENHEIGHT-60, SCREENWIDTH-40, 50)];
//    NSString *executableFile = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
//    bbhL.text = [NSString stringWithFormat:@"V %@",executableFile];
    bbhL.text = @"V 2.0.2022012701";
    bbhL.textAlignment = NSTextAlignmentRight;
    bbhL.font = [UIFont systemFontOfSize:13];
    bbhL.backgroundColor = [UIColor clearColor];
    bbhL.textColor = RGBCOLOR(100, 100, 100);
    bbhL.numberOfLines = 0;
    [self.view addSubview:bbhL];
    
    self.mbV.hidden = YES;
    self.srBgV.hidden = YES;
    
    
    [self getV];
    
    if (self.mmTF.text.length != 0) {
        [self loginBtnAction:nil];
    }
    
    [self.zhTF addTarget:self action:@selector(zhTFTextChange:) forControlEvents:UIControlEventEditingChanged];
    [self.mmTF addTarget:self action:@selector(mmTFTextChange:) forControlEvents:UIControlEventEditingChanged];
}
-(void)zhTFTextChange:(UITextField *)tf{
    if (tf.text.length > 0) {
        tf.rightViewMode = UITextFieldViewModeAlways;
    }
    if (tf.text.length == 0) {
        tf.rightViewMode = UITextFieldViewModeNever;
    }
}
-(void)mmTFTextChange:(UITextField *)tf{
    if (tf.text.length > 0) {
        tf.rightViewMode = UITextFieldViewModeAlways;
    }
    if (tf.text.length == 0) {
        tf.rightViewMode = UITextFieldViewModeNever;
    }
}
-(void)getV{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"苹果订货助手" forKey:@"strPlatform"];
    [params setObject:@"" forKey:@"strMemberCardNo"];
    [params setObject:@"T100" forKey:@"strVerify"];
    [params setObject:@"D1199" forKey:@"strVerifyPass"];
    NSString *uuidStr = [FCUUID uuidForVendor];
    [params setObject:uuidStr forKey:@"IMEI"];
    [NetTools postWithUrl:@"https://www.tudali.com:8802/App14.asmx/SelectVersion"  pararms:params success:^(id  _Nonnull json) {
        if ([[json objectForKey:@"Result"] boolValue] == YES) {
            NSString *Version = [NSString stringWithFormat:@"%@",[json  objectForKey:@"AppVersion"]];
            self.webURL = [NSString stringWithFormat:@"%@",[json objectForKey:@"FileUrl"]];
            NSString *NewInfo = [json objectForKey:@"NewInfo"];
            NSString *one = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
            if ([one floatValue] < [Version floatValue]) {
                if ([[json objectForKey:@"IsForce"] isEqualToString:@"Y"]) {
                    UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"更新提示" message:NewInfo preferredStyle:UIAlertControllerStyleAlert];
                    [ac addAction:[UIAlertAction actionWithTitle:@"立即升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.webURL]];
                        
                    }]];
                    [self presentViewController:ac animated:YES completion:^{
                        
                    }];
                }else{
                    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"更新提示" message:NewInfo preferredStyle:UIAlertControllerStyleAlert];
                    [alertC addAction:[UIAlertAction actionWithTitle:@"立即升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.webURL]];
                    }]];
                    [alertC addAction:[UIAlertAction actionWithTitle:@"稍后升级" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                        
//                        [self autoLogin];
                        
                    }]];
                    [self presentViewController:alertC animated:YES completion:^{
                        
                    }];
                }
            }else{
//                [self autoLogin];
            }
        }
            
            
        
    } fail:^(NSError * _Nonnull error) {
        NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"服务器的错误原因:%@",str);
        [SVProgressHUD dismiss];
    }];
}
//重新封装参数 加入app相关信息
- (NSString *)parseParams:(NSDictionary *)params
{
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithDictionary:params];

    NSString *keyValueFormat;
    NSMutableString *result = [NSMutableString new];
    //实例化一个key枚举器用来存放dictionary的key
   
   //加密处理 将所有参数加密后结果当做参数传递
   //parameters = @{@"i":@"加密结果 抽空加入"};
   
    NSEnumerator *keyEnum = [parameters keyEnumerator];
    id key;
    while (key = [keyEnum nextObject]) {
        keyValueFormat = [NSString stringWithFormat:@"%@=%@&", key, [params valueForKey:key]];
        [result appendString:keyValueFormat];
    }
    return result;
}
-(void)loginBtnAction:(UIButton *)btn{
    //登录
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showWithStatus:@"登录中"];
    if (self.srBgVTF.text.length == 0) {
        // 请求头http://www.zhenyea.com/WXSMTest/Api/OrderGoods UserLoginTest
//        NSString *accessPath = @"http://192.168.0.19:80/Api/OrderGoods/UserLogin";
//        NSString *accessPath = @"http://192.168.0.19:1444/Api/OrderGoods/UserLoginTest";
        NSString *accessPath = @"http://crm.zhenyea.com/Api/OrderGoods/UserLogin";
    
        // 请求参数字典
        NSDictionary *params = @{
            @"strUser":self.zhTF.text,
            @"strPassword":self.mmTF.text,
            @"strIMEI":[FCUUID uuidForVendor],
            @"strEquipmentInf":@"苹果"
        };
        
        //创建配置信息
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        //设置请求超时时间：5秒
        configuration.timeoutIntervalForRequest = 5;
        //创建会话
        NSURLSession *session = [NSURLSession sessionWithConfiguration: configuration delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
        NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",accessPath]]];
        //设置请求方式：POST
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"content-Type"];
        [request setValue:@"application/json;charset=gb2312" forHTTPHeaderField:@"Accept"];
        
        //data的字典形式转化为data
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
        //设置请求体
        [request setHTTPBody:jsonData];
        
        NSURLSessionDataTask * dataTask =[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            [SVProgressHUD dismiss];
            if (error == nil) {
                NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
                NSString *receiveStr = [[NSString alloc]initWithData:data encoding:encoding];
                NSData * aData = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
                NSError *errorr = nil;
                id response = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableContainers error:&errorr];
                if ([response isKindOfClass:[NSDictionary class]]) {
                    // 请求成功数据处理
                    NSString *IsResult = [NSString stringWithFormat:@"%@",[response objectForKey:@"IsResult"]];
                    if ([IsResult isEqualToString:@"1"]) {
                        [[NSUserDefaults standardUserDefaults] setObject:self.srBgVTF.text forKey:@"ipaddress"];
                        [[NSUserDefaults standardUserDefaults] setObject:response[@"Data"][@"IsShowPrice"] forKey:@"IsShowPrice"];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                        if (self.jzBtn.selected == NO) {
                            [[NSUserDefaults standardUserDefaults] setObject:self.zhTF.text forKey:@"zh"];
                            [[NSUserDefaults standardUserDefaults] setObject:self.mmTF.text forKey:@"mm"];
                            [[NSUserDefaults standardUserDefaults] synchronize];
                        }else{
                            [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"zh"];
                            [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"mm"];
                            [[NSUserDefaults standardUserDefaults] synchronize];
                        }
                        HomeViewController *hVC = [[HomeViewController alloc]init];
                        hVC.dataDic = (NSDictionary *)response[@"Data"];
                        hVC.dataDicPass = self.mmTF.text;
                        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:hVC];
                        nav.modalPresentationStyle = UIModalPresentationFullScreen;
                        [self presentViewController:nav animated:YES completion:nil];
                    }else{
                        [SVProgressHUD setMinimumDismissTimeInterval:0.2f];
                        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:[NSString stringWithFormat:@"%@",response[@"Message"]]];
                    }
                } else {

                }
            }else{
                NSLog(@"error===%@",error);
            }
        }];
        [dataTask resume];

//        AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        //如果报接受类型不一致请替换一致text/html或别的
//        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json; charset", @"text/html",@"text/json", @"text/javascript",@"text/plain",@"text/plain; charset=gb2312",nil];
//        NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:accessPath parameters:params error:nil];
//        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        
//        NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
//            [SVProgressHUD dismiss];
//            if (!error) {
//                if ([responseObject isKindOfClass:[NSDictionary class]]) {
//                    // 请求成功数据处理
//                    NSString *IsResult = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"IsResult"]];
//                    if ([IsResult isEqualToString:@"1"]) {
//                        [[NSUserDefaults standardUserDefaults] setObject:self.srBgVTF.text forKey:@"ipaddress"];
//                        [[NSUserDefaults standardUserDefaults] synchronize];
//                        if (self.jzBtn.selected == NO) {
//                            [[NSUserDefaults standardUserDefaults] setObject:self.zhTF.text forKey:@"zh"];
//                            [[NSUserDefaults standardUserDefaults] setObject:self.mmTF.text forKey:@"mm"];
//                            [[NSUserDefaults standardUserDefaults] synchronize];
//                        }else{
//                            [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"zh"];
//                            [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"mm"];
//                            [[NSUserDefaults standardUserDefaults] synchronize];
//                        }
//                        HomeViewController *hVC = [[HomeViewController alloc]init];
//                        hVC.dataDic = (NSDictionary *)responseObject[@"Data"];
//                        hVC.dataDicPass = self.mmTF.text;
//                        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:hVC];
//                        nav.modalPresentationStyle = UIModalPresentationFullScreen;
//                        [self presentViewController:nav animated:YES completion:nil];
//                    }else{
//                        [SVProgressHUD setMinimumDismissTimeInterval:0.2f];
//                        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:[NSString stringWithFormat:@"%@",responseObject[@"Message"]]];
//                    }
//                } else {
//
//                }
//            } else {
//                NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
//                NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//                NSLog(@"服务器的错误原因:%@",str);
//                [SVProgressHUD setMinimumDismissTimeInterval:0.2f];
//                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:[NSString stringWithFormat:@"%@",str]];
//            }
//        }];
//
//        [task resume];
    }else{
        NSDictionary *dic = @{
            @"strUser":self.zhTF.text,
            @"strPassword":self.mmTF.text,
            @"strIMEI":[FCUUID uuid],
            @"strEquipmentInf":@"苹果"
        };//@"http://192.168.0.19:60"///ldcy
        [NetTools getWithUrl:[NSString stringWithFormat:@"http://%@/ldcy/OrderGoods/OrderGoods.asmx/UserLogin",self.srBgVTF.text]  pararms:dic success:^(id  _Nonnull json) {
            [SVProgressHUD dismiss];
            if ([[NSString stringWithFormat:@"%@",json[@"IsResult"]] isEqualToString:@"1"]) {
                [[NSUserDefaults standardUserDefaults] setObject:self.srBgVTF.text forKey:@"ipaddress"];
                [[NSUserDefaults standardUserDefaults] setObject:false forKey:@"IsShowPrice"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                if (self.jzBtn.selected == NO) {
                    [[NSUserDefaults standardUserDefaults] setObject:self.zhTF.text forKey:@"zh"];
                    [[NSUserDefaults standardUserDefaults] setObject:self.mmTF.text forKey:@"mm"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                }else{
                    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"zh"];
                    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"mm"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                }
                HomeViewController *hVC = [[HomeViewController alloc]init];
                hVC.dataDic = (NSDictionary *)json[@"Data"];
                UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:hVC];
                nav.modalPresentationStyle = UIModalPresentationFullScreen;
                [self presentViewController:nav animated:YES completion:nil];
            }else{
                [SVProgressHUD setMinimumDismissTimeInterval:0.2f];
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:[NSString stringWithFormat:@"%@",json[@"Message"]]];
            }
        } fail:^(NSError * _Nonnull error) {
            NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
            NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"服务器的错误原因:%@",str);
            [SVProgressHUD dismiss];
        }];
    }
}
-(void)jzzhBtnAction:(UIButton *)btn{
    btn.selected = !btn.selected;
}
-(UITextField *)setupTFWithPlh:(NSString *)plh frame:(CGRect)frame{
    UITextField *tf = [[UITextField alloc]initWithFrame:frame];
    tf.textAlignment = NSTextAlignmentLeft;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentLeft;
    NSAttributedString *attri = [[NSAttributedString alloc] initWithString:plh attributes:@{NSForegroundColorAttributeName:RGBACOLOR(230, 230, 230, 1),NSFontAttributeName:[UIFont systemFontOfSize:15], NSParagraphStyleAttributeName:style}];
    tf.attributedPlaceholder = attri;
    tf.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:tf];
    CALayer *bottomBorder = [CALayer layer];
    bottomBorder.backgroundColor = [UIColor orangeColor].CGColor;
    bottomBorder.frame = CGRectMake(0.0f, tf.frame.size.height - 1, tf.frame.size.width, 1.0f);
    bottomBorder.backgroundColor = RGBCOLOR(230, 230, 230).CGColor;
    [tf.layer addSublayer:bottomBorder];
    if ([plh containsString:@"密码"]) {
        tf.secureTextEntry = YES;
        self.mmTF = tf;
//        self.mmTF.text = @"123";
        NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"mm"];
        self.mmTF.text = str.length==0?@"":str;
        UIView *vvv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
        UIImageView *imgVV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 30, 30)];
        imgVV.contentMode = UIViewContentModeScaleAspectFit;
        imgVV.image = [UIImage imageNamed:@"首页_07.png"];
        [vvv addSubview:imgVV];
        self.mmTF.leftView = vvv;
        self.mmTF.leftViewMode = UITextFieldViewModeAlways;
        UIButton *mmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        mmBtn.frame = CGRectMake(10, 10, 30, 30);
        [mmBtn setImage:[UIImage imageNamed:@"login2.png"] forState:UIControlStateNormal];
        [mmBtn setImage:[UIImage imageNamed:@"login3.png"] forState:UIControlStateSelected];
        UIView *rigttBgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        [rigttBgV addSubview:mmBtn];
        [mmBtn addTarget:self action:@selector(mmBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.mmTF.rightView = rigttBgV;
        self.mmTF.rightViewMode = UITextFieldViewModeAlways;
    }else{
        self.zhTF = tf;
//        self.zhTF.text = @"9999";
        NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"zh"];
        self.zhTF.text = str.length==0?@"":str;
        UIView *vvv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
        UIImageView *imgVV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 30, 30)];
        imgVV.contentMode = UIViewContentModeScaleAspectFit;
        imgVV.image = [UIImage imageNamed:@"首页_04.png"];
        [vvv addSubview:imgVV];
        self.zhTF.leftView = vvv;
        self.zhTF.leftViewMode = UITextFieldViewModeAlways;
        UIButton *zhBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        zhBtn.frame = CGRectMake(10, 10, 30, 30);
        [zhBtn setImage:[UIImage imageNamed:@"login1.png"] forState:UIControlStateNormal];
        UIView *rigttBgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        [rigttBgV addSubview:zhBtn];
        [zhBtn addTarget:self action:@selector(zhBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.zhTF.rightView = rigttBgV;
        self.zhTF.rightViewMode = UITextFieldViewModeAlways;
    }
    return tf;
}
-(void)mmBtnAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    self.mmTF.secureTextEntry = !self.mmTF.secureTextEntry;
}
-(void)zhBtnAction:(UIButton *)btn{
    self.zhTF.text = @"";
//    btn.hidden = YES;
    self.zhTF.rightViewMode = UITextFieldViewModeNever;
}
@end
